package org.agile.grenoble.client;

import java.sql.SQLException;

import javax.ws.rs.core.MultivaluedMap;

import org.agile.grenoble.questions.QuestionsDocument;
import org.agile.grenoble.questions.QuestionsType;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class AnswersStorageClient  {

	
	public void initializeDB(QuestionsType questions) throws SQLException {
		// TODO Auto-generated method stub

	}

	
	public void storeAnswers(QuestionsDocument doc, int pUserId)
			throws SQLException {
		
		Client c = Client.create();
		c.getProperties().put(ClientConfig.PROPERTY_FOLLOW_REDIRECTS, true);
		System.out.println("Client created, going to create a webresource") ;
		StringBuffer url =  new StringBuffer("http://localhost:9998/storeAnswers");
		
		WebResource webResource = c.resource(url.toString());
		MultivaluedMap<String, String> params = new MultivaluedMapImpl();
		params.add("answers", doc.xmlText());
		params.add("userId", ""+pUserId);

		
		webResource.type("application/x-www-form-urlencoded");
		
		System.out.println("WebResource created, getting the answer");
		//webResource.type("application/x-www-form-urlencoded").post("storeAnswers", params);
		ClientResponse response = webResource.post(ClientResponse.class, params);
		//WebResource resource = r.path("storeAnswers").queryParams(params);

		
		//Builder builder = resource.accept(MediaType.APPLICATION_JSON_TYPE,MediaType.TEXT_PLAIN_TYPE) ;
		//Builder b1 = builder.header("X-FOO", "BAR");
		//String response = b1.get(String.class);
		
		
		System.out.println("Response is :" + response ) ;		
	}

}
